package com.example.myprojectoffeixue;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

//本界面是选择要添加的遥控器的设备类型界面，
// 主要逻辑是根据点击的条目不同从而传递不同的flag
public class AddHongWaiSheBeiActivity extends Activity {
    private ArrayList<HongWaiSB> parseArray=new ArrayList<HongWaiSB>();
    private ListView lv;
    private MyApplication m;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_hong_wai_she_bei);
    }

    @Override
    protected void onStart() {
        info();
        super.onStart();
    }
    private void info() {
        m = (MyApplication) getApplication();
        lv = (ListView) findViewById(R.id.lv_addhongwai);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        httpUtils.send(HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/remote/getKind", params,
                new RequestCallBack<String>() {
                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        ToastUtils.showToast(AddHongWaiSheBeiActivity.this,"网络异常");
                    }
                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                       if( arg0.result != null && !"".equals(arg0.result)){
                        arg0.result = AESUtil.decrypt(arg0.result);
                        JSONObject j = JSONObject.parseObject(arg0.result);
                        String status=j.getString("status");
                        if("0".equals(status)){
                        JSONArray j1 = j.getJSONArray("controllerList");
                        parseArray = (ArrayList<HongWaiSB>) JSONObject.parseArray(j1.toString(), HongWaiSB.class);
                        HWadapter h = new HWadapter(parseArray, AddHongWaiSheBeiActivity.this, 1);
                        lv.setAdapter(h);}else{

                            ToastUtils.showToast(AddHongWaiSheBeiActivity.this,"服务器错误");
                        }
                    }else{
                           ToastUtils.showToast(AddHongWaiSheBeiActivity.this,"服务器错误");
                       }
                    }
                });


        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 2) {

                    ToastUtils.showToast(AddHongWaiSheBeiActivity.this,"功能敬请期待");
                } else {

                    Intent it = new Intent(AddHongWaiSheBeiActivity.this, HongWStudyActivity.class);
                    it.putExtra("flag", parseArray.get(position).selectFlag);
                    m.type = parseArray.get(position).kind;
                    startActivity(it);
                }

            }
        });


    }


}
